<template>
  <div>
    <div class="add_table_css">
      <el-button plain @click="addEducation" class="btn_css_newadd">新增工作信息</el-button>
      <!-- 新增工作信息的弹框 -->
      <el-dialog title="新增工作信息" :visible.sync="dialogFormVisible" width="40%" center>
        <el-form
          label-width="150px"
          :model="workInfoTable"
          :rules="rules"
          ref="workInfoTable"
          class="demo-ruleForm add_education_css"
        >
          <el-form-item label="起始时间" prop="startingTime">
            <!-- <el-date-picker
              type="date"
              placeholder="1987-09-23"
              v-model="workInfoTable.startingTime"
            ></el-date-picker>-->
            <el-date-picker
              v-model="workInfoTable.startingTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <!-- <el-date-picker type="date" placeholder="1987-09-23" v-model="workInfoTable.endTime"></el-date-picker> -->
            <el-date-picker
              v-model="workInfoTable.endTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="workInfoTable.unit"></el-input>
          </el-form-item>
          <el-form-item label="从事专业技术工作" prop="technicalWork">
            <el-input type="textarea" v-model="workInfoTable.technicalWork"></el-input>
          </el-form-item>
          <el-form-item label="职务" prop="job">
            <el-input v-model="workInfoTable.job"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer add_footer_btn_css">
          <el-button plain @click="addOk('workInfoTable')" class="btn_css_bord">保存</el-button>
          <el-button plain @click="dialogFormVisible = false" class="btn_css_bord">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 表格 -->
      <el-container>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index" width="50"></el-table-column>
          <el-table-column prop="startingTime" label="起始时间" width="120"></el-table-column>
          <el-table-column prop="endTime" label="结束时间" width="120"></el-table-column>
          <el-table-column prop="unit" label="单位" width="120"></el-table-column>
          <el-table-column prop="technicalWork" label="从事专业技术工作" width="280"></el-table-column>
          <el-table-column prop="job" label="职务" width="100"></el-table-column>
          <el-table-column label="操作" width="170">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_er"
              >修改</el-button>
              <el-button
                @click.native.prevent="del(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_er"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-container>
      <!-- 修改工作信息的弹框 -->
      <el-dialog title="修改工作信息" :visible.sync="dialogFormVisible2" width="40%" center>
        <el-form
          label-width="150px"
          :model="workInfoTable2"
          :rules="rules"
          ref="workInfoTable2"
          class="demo-ruleForm add_education_css"
        >
          <el-form-item label="起始时间" prop="startingTime">
            <!-- <el-date-picker
              type="date"
              placeholder="1987-09-23"
              v-model="workInfoTable2.startingTime"
            ></el-date-picker>-->
            <el-date-picker
              v-model="workInfoTable2.startingTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <!-- <el-date-picker type="date" placeholder="1987-09-23" v-model="workInfoTable2.endTime"></el-date-picker> -->
            <el-date-picker
              v-model="workInfoTable2.endTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="workInfoTable2.unit"></el-input>
          </el-form-item>
          <el-form-item label="从事专业技术工作" prop="technicalWork">
            <el-input type="textarea" v-model="workInfoTable2.technicalWork"></el-input>
          </el-form-item>
          <el-form-item label="职务" prop="job">
            <el-input v-model="workInfoTable2.job"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer add_footer_btn_css">
          <el-button plain @click="updateOk('workInfoTable2')" class="btn_css_bord">保存</el-button>
          <el-button plain @click="dialogFormVisible2 = false" class="btn_css_bord">关闭</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 分页 -->
    <div class="block page_css">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="limit"
        @current-change="change"
        @prev-click="prev"
        @next-click="next"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      staffId: 1,
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      workInfoTable: {
        unit: "",
        technicalWork: "",
        job: "",
        startingTime: "",
        endTime: ""
      },
      workInfoTable2: {},
      rules: {
        unit: [{ required: true, message: "请输入毕业院校", trigger: "blur" }],
        technicalWork: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        job: [{ required: true, message: "请输入毕业院校", trigger: "blur" }],
        startingTime: [
          { required: true, message: "请选择入学时间", trigger: "change" }
        ],
        endTime: [
          { required: true, message: "请选择毕业时间", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    /* 页面渲染 */
    getCon() {
      this.$axios({
        url: "/workinformationsheet/findByWhere",
        params: {
          staffId: this.staffId,
          page: this.page,
          limit: this.limit
        }
      }).then(res => {
        // console.log(111, res.data.data);
        this.tableData = res.data.data;
        this.total = res.data.count;
      });
    },
    /* 分页渲染 */
    change(page) {
      this.page = page;
      this.getCon();
    },
    prev() {
      this.page--;
      this.getCon();
    },
    next() {
      this.page++;
      this.getCon();
    },
    /* 添加 */
    addEducation() {
      this.dialogFormVisible = true;
    },
    addOk(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          this.$axios({
            url: "/workinformationsheet/add",
            method: "post",
            data: {
              staffId: this.staffId,
              startingTime: this.workInfoTable.startingTime,
              endTime: this.workInfoTable.endTime,
              unit: this.workInfoTable.unit,
              technicalWork: this.workInfoTable.technicalWork,
              job: this.workInfoTable.job
            }
          }).then(res => {
            console.log(res);
            this.getCon();
            this.dialogFormVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* 修改 */
    update(index, data) {
      this.dialogFormVisible2 = true;
      this.workInfoTable2 = { ...data[index]};
      // console.log(data[index]);
    },
    updateOk(formName) {
      console.log(this.workInfoTable2.wId)
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios({
            url: "/workinformationsheet/update",
            method: "post",
            data: {
              wId: this.workInfoTable2.wId,
              unit: this.workInfoTable2.unit,
              technicalWork: this.workInfoTable2.technicalWork,
              job: this.workInfoTable2.job,
              startingTime: this.workInfoTable2.startingTime,
              endTime: this.workInfoTable2.endTime
            }
          }).then(res => {
            console.log(res);
            this.getCon();
            this.dialogFormVisible2 = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* 删除 */
    del(index, data) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$axios({
          url:'/workinformationsheet/delete',
          method:'post',
          data:{
            wId:data[index].wId,
          }
        }).then(res => {
          console.log(res);
          this.getCon();
        })
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },




    handleClick(row) {
      this.dialogFormVisible2 = true;
      console.log(row);
    },

    

    save() {
      this.dialogFormVisible = false;
    }
  },

  mounted() {
    this.getCon();
    sessionStorage.setItem("fName", "Process2");
  }
};
</script>
<style scoped lang="less">
.page_css {
  margin: 10px;
  text-align: right;
}
.el-table {
  font-size: 13px;
}
.btn_css_newadd {
  background-color: #f7f7f7;
  border: 1px solid #eee;

  text-align: center;
}
.btn_css_er {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 60px;
  height: 30px;
  text-align: center;
}
.btn_css_bord {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 80px;
  height: 40px;
  font-size: 14px;
  text-align: center;
}
.add_education_css {
  width: 370px;
  margin-left: 40px;
}
.add_footer_btn_css {
  margin: 0 auto;
}
.add_table_css {
  border: 1px solid #eee;
  // height: 600px;
  padding: 20px;
}
</style>